$(function () {
    var setting = {
        async: {
            enable: true,
            url: "/content/showContentCategory",
            autoParam: ["id"],
            dataFilter: filter,

        },
        view: {
            addHoverDom: addHoverDom,
            removeHoverDom: removeHoverDom,
            selectedMulti: false
        },
        edit: {
            enable: true,
            editNameSelectAll: true,
            showRemoveBtn: true,
            showRenameBtn: true
        },
        callback: {
            beforeDrag: beforeDrag,
            beforeEditName: beforeEditName,
            beforeRename: beforeRename,
            onRemove: zTreeOnRemove,
            beforeRemove: zTreeBeforeRemove,
            onRename: zTreeOnRename
        }
    };

    function beforeDrag(treeId, treeNodes) {
        return false;
    }

    function beforeEditName(treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("contentCategoryTree");
        zTree.selectNode(treeNode);
        zTree.editName(treeNode);
        return false;
    }

//删除
    function zTreeBeforeRemove(treeId, treeNode) {
        var isUpate = confirm("确定删除吗？");
        if (isUpate) {
            return true;
        }
        return false;
    }

    function zTreeOnRemove(event, treeId, treeNode) {
        $.ajax({
            type: "post",
            url: "/content/deleteContentCategory",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(treeNode),
            success: function (res) {
                console.log(res.msg);

            }
        });
    }

    //修改
    function beforeRename(treeId, treeNode, newName, isCancel) {


        if (newName.length == 0) {
            setTimeout(function () {
                var zTree = $.fn.zTree.getZTreeObj("contentCategoryTree");
                zTree.cancelEditName();
                alert("节点名称不能为空.");
            }, 0);
            return false;
        }
        var isUpdate = confirm("确定修改吗？");
        if (isUpdate) {
            return true;
        }

        return false;
    }

    function zTreeOnRename(event, treeId, treeNode, isCancel) {

        $.ajax({
            type: "post",
            url: "/content/updateContentCategory",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(treeNode),

            success: function (res) {
                console.log(res.msg);
            }

        })
        ;

    }


    var newCount = 1;

    //增加
    function addHoverDom(treeId, treeNode) {
        var sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
        var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
            + "' title='add node' onfocus='this.blur();'></span>";
        sObj.after(addStr);
        var btn = $("#addBtn_" + treeNode.tId);
        if (btn) btn.bind("click", function () {
            var zTree = $.fn.zTree.getZTreeObj("contentCategoryTree");
            //添加节点触发事件

            zTree.addNodes(treeNode, {id: (100 + newCount), pId: treeNode.id, name: "new node" + (newCount++)});
            $.ajax({
                type: "post",
                url: "/content/addContentCategory",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(treeNode),
                success: function (res) {
                    console.log(res.msg);
                }

            })
            ;
            return false;
        });
    };

    //鼠标移动到上面 展示图标 并且可以获取到当前节点id
    function removeHoverDom(treeId, treeNode) {

        $("#addBtn_" + treeNode.tId).unbind().remove();

    };

    function selectAll() {
        var zTree = $.fn.zTree.getZTreeObj("contentCategoryTree");
        zTree.setting.edit.editNameSelectAll = $("#selectAll").attr("checked");
    }


    function filter(treeId, parentNode, childNodes) {
        if (!childNodes) return null;
        for (var i = 0, l = childNodes.length; i < l; i++) {
            childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
        }
        return childNodes;
    }

    //初始化树形结构
    $.fn.zTree.init($("#contentCategoryTree"), setting);
    $("#selectAll").bind("click", selectAll);
    layui.use(['layer'], function () {
        var layer = layui.layer;

        $("#contentCategory").click(function () {
            $("#treeBox").show();

        })
    });

})